<template>
  <div class="page-user-balance">
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('user:userBalance:save') && dataList.length < 10"
          class="default-btn primary-btn"
          @click.stop="onAddOrUpdate()"
        >
          {{ $t("product.addNew") }}
        </div>
      </div>
      <div class="table-con user-balance-table">
        <el-table
          ref="userBalanceTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            :label="$t('number')"
            width="80"
          />
          <el-table-column
            prop="balanceTitle"
            :label="$t('distributionMsg.title')"
            width="320"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.balanceTitle }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="rechargeAmount"
            :label="$t('user.rechargeAmount')"
            width="auto"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.rechargeAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="img"
            :label="$t('user.bgImg')"
            width="auto"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <img-show :src="scope.row.img" />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="seq"
            :label="$t('temp.sequence')"
            width="auto"
          />

          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="180"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('user:userBalance:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.balanceId)"
                >
                  {{ $t('groups.edit') }}
                </div>
                <div
                  v-if="isAuth('user:userBalance:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row.balanceId)"
                >
                  {{ $t('text.delBtn') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { isAuth } from '@/utils/index'
const dataList = ref([])
const dataListLoading = ref(false)
onMounted(() => {
  onGetDataList()
})
const onGetDataList = () => {
  dataListLoading.value = true
  http({
    url: http.adornUrl('/user/userBalance/getList'),
    method: 'get',
    params: http.adornParams()
  }).then(({ data }) => {
    dataList.value = data
    dataListLoading.value = false
  })
}
// 新增 / 修改
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}
const onDelete = (id) => {
  ElMessageBox.confirm($t('admin.isDeleOper'), $t('text.tips'), {
    confirmButtonText: $t('coupon.confirm'),
    cancelButtonText: $t('coupon.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/user/userBalance/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      ElMessage({
        message: $t('groups.successfulOperation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  }).catch(() => { })
}
/**
 * 刷新回调
 */
const refreshChange = () => {
  onGetDataList()
}

</script>
<style lang="scss" scoped>
.page-user-balance {
  .main-container {
    margin-top: 0;
    padding-top: 0;
  }
}
</style>
